/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.rest;

import co.yixiang.api.YshopException;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.appdict.domain.Appdict;
import co.yixiang.modules.appdict.service.mapper.AppdictMapper;
import co.yixiang.modules.auction.domain.AuctionRecord;
import co.yixiang.modules.auction.service.AuctionRecordService;
import co.yixiang.modules.auction.service.dto.AuctionRecordDto;
import co.yixiang.modules.auction.service.dto.AuctionRecordQueryCriteria;
import co.yixiang.modules.auction.service.dto.SingleDto;
import co.yixiang.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
* @author ycl
* @date 2021-02-18
*/
@AllArgsConstructor
@Api(tags = "拍卖专场配置管理")
@RestController
@RequestMapping("/api/AuctionRecord")
public class AuctionRecordController {

    private final AuctionRecordService AuctionRecordService;
    private final AppdictMapper appdictMapper;
    private final RedisUtils redisUtil;
    private final IGenerator generator;


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('admin','AuctionRecord:list')")
    public void download(HttpServletResponse response, AuctionRecordQueryCriteria criteria) throws IOException {
        AuctionRecordService.download(generator.convert(AuctionRecordService.queryAll(criteria), AuctionRecordDto.class), response);
    }

    @GetMapping
    @Log("查询拍卖专场配置")
    @ApiOperation("查询拍卖专场配置")
//    @PreAuthorize("@el.check('admin','AuctionRecord:list')")
    public ResponseEntity<Object> getAuctionRecords(AuctionRecordQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(AuctionRecordService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增拍卖专场配置")
    @ApiOperation("新增拍卖专场配置")
//    @PreAuthorize("@el.check('admin','AuctionRecord:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody AuctionRecord resources){
        if(resources.getPrice().compareTo(resources.getTopPrice()) == 1) throw new YshopException("封顶价需大于拍卖价");
        //获取手续费比例
        Appdict appdict=appdictMapper.getByCode("sxf");
        //加上0.5%的积分比
        BigDecimal add = new BigDecimal(appdict.getMemo());
        resources.setServiceCharge(resources.getPrice().multiply(add));
        AuctionRecordService.save(resources);
        redisUtil.set("paimai"+resources.getId(),resources.getId());
        resources.setUpId(resources.getId());
        AuctionRecordService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改拍卖专场配置")
    @ApiOperation("修改拍卖专场配置")
//    @PreAuthorize("@el.check('admin','AuctionRecord:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody AuctionRecord resources){
        if(resources.getPrice().compareTo(resources.getTopPrice()) == 1) throw new YshopException("封顶价需大于拍卖价");
        Appdict appdict=appdictMapper.getByCode("sxf");
        //加上0.5%的积分比
        BigDecimal add = new BigDecimal(appdict.getMemo());
        resources.setServiceCharge(resources.getPrice().multiply(add));
        AuctionRecordService.updateById(resources);
        redisUtil.set("paimai"+resources.getId(),resources.getId());
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除拍卖专场配置")
    @ApiOperation("删除拍卖专场配置")
//    @PreAuthorize("@el.check('admin','AuctionRecord:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        Arrays.asList(ids).forEach(id->{
            AuctionRecordService.removeById(id);
        });
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("获取拍卖商品")
    @ApiOperation("获取拍卖商品")
    @GetMapping("/getAuctionProduct")
    public ResponseEntity<Object> getAuctionProduct() {
        return new ResponseEntity<>(AuctionRecordService.getAuctionProduct() ,  HttpStatus.OK);
    }


    @Log("获取拍卖商品")
    @ApiOperation("获取拍卖商品")
    @GetMapping("/confirm")
    public ResponseEntity confirm(@RequestParam(value = "id") Long  id) {
        return new ResponseEntity<>(AuctionRecordService.confirmAuction(id) ,  HttpStatus.OK);
    }


    @Log("获取当天拍卖商品")
    @ApiOperation("获取当天拍卖商品")
    @GetMapping("/singleList")
    public ResponseEntity singleList(@RequestParam(value = "productName") String productName) {
        return new ResponseEntity<>(AuctionRecordService.singleList(productName) ,  HttpStatus.OK);
    }

    @Log("一键收单")
    @ApiOperation("一键收单")
    @PostMapping("/confirmSingle")
    public ResponseEntity confirmSingle(@RequestBody List<SingleDto> singleDtoList, @RequestParam(value = "uid") Long uid) {
        AuctionRecordService.confirmSingle(singleDtoList , uid) ;
        return new ResponseEntity<>( HttpStatus.OK);
    }


}
